import {localeProps, makeRequiredRule} from '../../utils';
import unique from "@form-create/utils/lib/unique";

const label = '表格表单';
const name = 'tableForm';

export default {
  icon: 'icon-table-form',
  label,
  name,
  subForm: 'array',
  children: 'table-form-column',
  mask: false,
  loadRule(rule) {
    if (!rule.props) {
      rule.props = {};
    }
    const columns = rule.props.columns || [];
    rule.children = columns.map(column => {
      return {
        type: 'tableFormColumn',
        _fc_drag_tag: 'tableFormColumn',
        props: {
          label: column.label,
          width: column.style.width || '',
          color: column.style.color || '',
        },
        children: column.rule || []
      }
    });
    delete rule.props.columns;
  },
  parseRule(rule) {
    const children = rule.children || [];
    rule.props.columns = children.map(column => {
      return {
        label: column.props.label,
        style: {
          width: column.props.width,
          color: column.props.color,
        },
        rule: column.children || []
      };
    })
    rule.children = [];
  },
  rule({t}) {
    return {
      type: name,
      field: unique(),
      title: t('components.tableForm.name'),
      info: '',
      props: {},
      children: []
    };
  },
  props(_, {t}) {
    return localeProps(t, name + '.props', [
      makeRequiredRule(),
      {
        type: 'switch',
        title: '是否禁用',
        field: 'disabled'
      },
      {
        type: 'inputNumber',
        title: '最多添加几行，为 0 则不限制',
        field: 'max',
        props: {min: 0}
      },
    ]);
  }
};
